<?php
/**
 * 常用函数
 *
 */
class Common {
    
    /**
     * alert提示信息,并做跳转
     * @param 提示的信息 $msg
     * @param 要跳转到的路径 $href
     * @param 是否终止当前脚本 $exit
     */
    static function msg($msg, $href = '', $exit = 'true') {
        header("Content-type:   text/html;   charset=utf-8");
        if ($href == '') {
            $href = $_SERVER['HTTP_REFERER'];
        }
        if ($msg) {
            $alert_msg = "alert('$msg');";
            echo "<script type='text/javascript'>$alert_msg window.location.href = '".$href."'</script>";
        } else {
            echo "<script type='text/javascript'>window.location.href = '".$href."'</script>";
        }
        
        if ($exit == 'true') {
            exit();
        }
    }
    

    /**
     * 创建缩略图
     *
     * @param 源文件路径 $srcFile
     * @param 保存路径 $dstFile
     * @param 宽度 $dstW
     * @param 高度 $dstH
     * @param 是否等比缩放 $fill
     * @return bool
     */
    static function createThumb($srcFile, $dstFile, $dstW, $dstH, $fill = true){
        $data = getimagesize($srcFile);
        switch ($data[2]) {
            case 1:
                $srcImg = imagecreatefromgif($srcFile);
                break;
            case 2:
                $srcImg = imagecreatefromjpeg($srcFile);
                break;
            case 3:
                $srcImg = imagecreatefrompng($srcFile);
                break;
			case 6:
                $srcImg = imagecreatefrom($srcFile);
                break;
            default:
                return;
                break;
        }
        if ($srcImg) {
            $srcW = imagesx($srcImg);
            $srcH = imagesy($srcImg);
            $dstX = 0;
            $dstY = 0;
            
            if ($srcW * $dstH > $srcH * $dstW) {// srcW/srcH > dstW/dstH ，源比目标扁
                $dstRealW = $dstW;
                $dstRealH = round($srcH * $dstRealW / $srcW);
            } else {
                $dstRealH = $dstH;
                $dstRealW = round($srcW * $dstRealH / $srcH);
            }
            
            if ($fill) {
                $dstX = floor(($dstW - $dstRealW) / 2);
                $dstY = floor(($dstH - $dstRealH) / 2);
                $dstImg = imagecreatetruecolor($dstW, $dstH);
                $backColor = imagecolorallocate($dstImg, 250, 250, 250);//缩图空出部分的背景色
                imagefilledrectangle($dstImg, 0, 0, $dstW, $dstH, $backColor);
            } else {
                $dstImg = imagecreatetruecolor($dstRealW, $dstRealH);   
            }
            
            //ImageCopyResized($dstImg, $srcImg, 0, 0, 0, 0, $dstRealW, $dstRealH, $srcW, $srcH);
            imagecopyresampled($dstImg, $srcImg, $dstX, $dstY, 0, 0, $dstRealW, $dstRealH, $srcW, $srcH);
    
            imagejpeg($dstImg, $dstFile, 85);
            chmod($dstFile, 0644);
            imagedestroy($srcImg);
            imagedestroy($dstImg);
            return true;
        }
        return false;
    }
    
    /**
     * 返回取出http://后的地址
     *
     * @param string $website
     * @return string
     */
    static function pregDomain($website) {
        if($website == '直接输入'){
            return '直接输入';
        }
        preg_match("/^(http:\/\/)?([^\/]+)/i", $website, $matches);
        return  $matches[2];
    }

    static function getMainDomain($domain) {
        return str_replace(array('www.','bbs.','blog.','boke.'),'' ,self::pregDomain($domain));
    }

    /**
     * 获取加密密码
     * @param <String> $str
     */
    static function getPassword($str) {
        return md5(md5($str).substr(md5($str),0, 3).substr($str, 2));
    }
}
